//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
// $Id$
//
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

#include "MStackingAction.hh"

#include "G4ParticleDefinition.hh"
#include "G4ParticleTypes.hh"
#include "G4Track.hh"
#include "G4ios.hh"
#include "G4VProcess.hh"
#include "TH1F.h"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

MStackingAction::MStackingAction()
: gammaCounter(0)
{
    double QE[25]={0.34461,0.428418,0.3675,0.2852,0.2212,0.1756,0.1360,0.1025,0.07218,0.05549,0.04489,0.03431,0.02372,0.01618,0.008638,0.0056712,0.0027077,0.0028,0.0013554,0};
    //double QE[14] = {0.45, 0.40, 0.35, 0.32, 0.29, 0.27, 0.22, 0.20, 0.14, 0.09, 0.04, 0.02, 0.001, 0.0};
    fQE = new TH1F("fQE", "fQE", 25, 120, 210);
    for (int i = 0; i < 25; i++)
        fQE->SetBinContent(i, QE[i]);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

MStackingAction::~MStackingAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

G4ClassificationOfNewTrack
MStackingAction::ClassifyNewTrack(const G4Track * aTrack)
{
  G4double x = aTrack->GetPosition().x();
  G4double y = aTrack->GetPosition().y();
  G4double z = aTrack->GetPosition().z();

  G4int trackID = aTrack->GetTrackID();

  G4double length=aTrack->GetTrackLength();
  
  if(aTrack->GetDefinition() == G4OpticalPhoton::OpticalPhotonDefinition())
  { // particle is optical photon
    if(aTrack->GetParentID()>0)
    { // particle is secondary
      G4double waveLen = 0.0012398 / aTrack->GetKineticEnergy();
            double qe = fQE->Interpolate(waveLen);
            if (G4UniformRand() > qe)
            {
                return fKill;
            }
      gammaCounter++;
    }
  }
//  G4VPhysicalVolume * thePostPV = thePostPoint->GetPhysicalVolume();
//  if (!thePostPV) {G4cout << "OUT OF WORLD" << endl; return;}
//  G4String thePostPVname = thePostPV->GetName();
//  G4cout << "PostPV name : " << thePostPVname << endl;
//  if(thePostPVname(0,2)!="SD") { G4cout << "Hit SD!" << G4endl; 



  if (aTrack -> GetDefinition() == G4OpticalPhoton::OpticalPhotonDefinition()/* && aTrack -> GetVolume() -> GetName() == "PEC_Phys"*/) {  /*return fKill;*/}
  //if (/*aTrack -> GetDefinition() -> GetParticleName() == "opticalphoton"  && */aTrack -> GetVolume() -> GetName()) { G4cout << "YES!" << "\t" << aTrack -> GetVolume() << G4endl; /*return fKill;*/}
  return fUrgent;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void MStackingAction::NewStage()
{
 // G4cout << "Number of optical photons produced in this event : "
 //        << gammaCounter << G4endl;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void MStackingAction::PrepareNewEvent()
{ gammaCounter = 0; }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
